package com.cmcc.passenger.ui.tripoption.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.cmcc.passenger.data.entity.response.Gratuity
import com.cmcc.passenger.databinding.ListItemTripOptionButtonBinding
import com.cmcc.shared.utils.clickWithTrigger

/**
 * Author: jiangxin
 * Date: 2019/11/14
 * Description:
 */
class OptionTipAdapter :
    ListAdapter<Gratuity, OptionTipAdapter.ViewHolder>(OptionTipDiffCallback()) {
    private var checkedPosition = -1

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            ListItemTripOptionButtonBinding.inflate(
                LayoutInflater.from(
                    parent.context
                ), parent, false
            )
        )
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val model = getItem(position)
        holder.binding.carAny.text = model.name
        holder.bind(model)
        if (checkedPosition == -1) {
            holder.itemView.isSelected = false
        } else {
            holder.itemView.isSelected = checkedPosition == position
        }
        holder.itemView.clickWithTrigger {
            if (checkedPosition != position) {
                holder.itemView.isSelected = true
                checkedPosition = position
                notifyDataSetChanged()
            }
        }
    }

    inner class ViewHolder(val binding: ListItemTripOptionButtonBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(model: Gratuity) {
        }
    }

}


class OptionTipDiffCallback : DiffUtil.ItemCallback<Gratuity>() {
    override fun areItemsTheSame(oldItem: Gratuity, newItem: Gratuity): Boolean {
        return oldItem.id == newItem.id
    }

    override fun areContentsTheSame(oldItem: Gratuity, newItem: Gratuity): Boolean {
        return oldItem == newItem
    }

}